package cz.hanis.android.pubaccounting;

import java.util.List;
import cz.hanis.android.pubaccounting.model.Pub;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PubListAdapter extends ArrayAdapter<Pub> {

	public PubListAdapter(Context context, List<Pub> pubs) {
		super(context, R.layout.pub_list_item, R.id.pub_list_label, pubs);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View listItem = super.getView(position, convertView, parent);
		Pub pub = getItem(position);

		TextView label = null;
		ImageView icon = null;
		ViewHolder holder = (ViewHolder) listItem.getTag();
		if (holder != null) {
			label = holder.textView;
			icon = holder.imageView;
		} else {
			label = (TextView) listItem.findViewById(R.id.pub_list_label);
			icon = (ImageView) listItem.findViewById(R.id.pub_list_icon);
			holder = new ViewHolder(label, icon);
			listItem.setTag(holder);
		}
		label.setText(pub.getName());
		icon.setImageResource(R.drawable.beer);
		return (listItem);
	}

	private static class ViewHolder {
		protected final TextView textView;
		protected final ImageView imageView;

		public ViewHolder(TextView textView, ImageView imageView) {
			this.textView = textView;
			this.imageView = imageView;
		}
	}

}
